/**
* Copyright 2023 PYSTOR DBS, Nanjing Pengyun Network Technology Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ 

package py.dd.processor;

import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import py.common.PyService;
import py.dd.common.ServicePort;
import py.test.TestBase;

/**
 * A class contains some tests for {@link ShutdownServiceProcessor}.
 */
public class ShutdownServiceProccessorTest extends TestBase {

  private static final Logger logger = LoggerFactory.getLogger(ShutdownServiceProccessorTest.class);

  @Override
  public void init() throws Exception {
    super.init();
  }

  /**
   * In this test plan, service port in request is not filled and default service port will be
   * returned.
   */
  @Test
  public void testGetDefaultServicePort() throws Exception {
    ShutdownServiceProcessor shutdownServiceProcessor = new ShutdownServiceProcessor();
    int servicePort = shutdownServiceProcessor.getServicePort(PyService.DIH.getServiceName(), 0);
    Assert.assertEquals(ServicePort.DIH.getValue(), servicePort);
  }

  /**
   * In this test plan, service port in request is filled and it will be returned.
   */
  @Test
  public void testGetServicePort() throws Exception {
    final int servicePortInRequest = 10001;

    ShutdownServiceProcessor shutdownServiceProcessor = new ShutdownServiceProcessor();
    int servicePort = shutdownServiceProcessor
        .getServicePort(PyService.DIH.getServiceName(), servicePortInRequest);
    Assert.assertEquals(servicePortInRequest, servicePort);
  }

  /**
   * In this test plan, it gets service port with an unrecognized service.
   */
  @Test
  public void testGetServicePortWithUnrecognizedService() throws Exception {
    ShutdownServiceProcessor shutdownServiceProcessor = new ShutdownServiceProcessor();
    try {
      shutdownServiceProcessor.getServicePort("", 0);
      Assert.fail();
    } catch (IllegalArgumentException e) {
      logger.error("", e);
    }
  }
}
